<template>
  <div>
    <el-page-header @back="goBack" content="编辑用户" style="color:#3198ff"></el-page-header>
    <div
      v-loading="loading"
      element-loading-text="拼命加载中"
      element-loading-spinner="el-icon-loading"
      class="box"
    >
      <h4>基本信息</h4>
      <el-form :model="form" :rules="rules" ref="formRef">
        <el-row>
          <el-col :span="12">
            <el-form-item label="用户姓名" prop="consumername" :label-width="formLabelWidth">
              <el-input v-model="form.consumername"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="职务" prop="duties" :label-width="formLabelWidth">
              <el-input v-model="form.duties"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="登录账号" prop="username" :label-width="formLabelWidth">
              <el-input v-model="form.username" disabled></el-input>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item label="密码" prop="password" :label-width="formLabelWidth">
              <el-input v-model="form.password" show-password></el-input>
            </el-form-item>
          </el-col>-->
        </el-row>
        <el-form-item label="状态" prop="status1" :label-width="formLabelWidth">
          <el-switch v-model="form.status1"></el-switch>
        </el-form-item>
        <el-form-item label="备注" prop="description" :label-width="formLabelWidth">
          <el-input type="textarea" :rows="4" v-model="form.description"></el-input>
        </el-form-item>
        <el-form-item label="店铺" prop="storeId" :label-width="formLabelWidth">
          <el-select v-model="form.storeId" placeholder="请选择">
            <el-option
              v-for="item in productOptions"
              :key="item.toreId"
              :label="item.name"
              :value="item.toreId"
            ></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item :label-width="formLabelWidth">
      <el-button plain @click="resetForm('formRef')">取消</el-button>
      <el-button type="primary" @click="submitForm('formRef')">确定</el-button>
        </el-form-item>-->
      </el-form>
      <h4>配置角色</h4>
      <div>
        <el-table
          ref="multipleTable"
          :data="tableData"
          tooltip-effect="dark"
          style="width: 100%"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="index" :index="indexMethod" label="序号" width="100"></el-table-column>
          <el-table-column prop="roleName" label="角色名称" width="300"></el-table-column>
          <el-table-column prop="remark" label="角色说明"></el-table-column>
          <el-table-column type="selection" width="80"></el-table-column>
        </el-table>
      </div>
      <div style="text-align:center; margin-top: 50px;">
        <el-button @click="backClick">取消</el-button>
        <el-button type="primary" @click="submitForm('formRef')">保存</el-button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      productOptions: [],
      form: {
        duties: "",
        consumername: "",
        username: "",
        password: "",
        description: "",
        status1: false,
        storeId:"",
      },
      loading: true,
      tableData: [],
      multipleSelection: [],
      datas: {},
      formLabelWidth: "120px",
      rules: {
        consumername: [
          { required: true, message: "请输入用户姓名", trigger: "blur" }
        ],
        username: [
          { required: true, message: "请输入登陆账号", trigger: "blur" },
          { min: 4, max: 15, message: "长度在 4 到 15 个字符", trigger: "blur" }
        ],
        duties: [{ required: true, message: "请输入职务", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }]
        // password: [{ required: true, message: "请输入密码", trigger: "blur" }]
      }
    };
  },
  mounted() {
    var username = this.$route.query.name;

    this.$service.getRoleList().then(res => {
      //console.log(res);
      this.tableData = res.data;
      this.$service.getUserDetail(username).then(result => {
        //console.log(result);
        this.loading = false;
        result.status1 = result.status == "1" ? true : false;
        this.form = result;
        var rows = [];

        if (result.roleId != null) result.roleId = result.roleId.split(",");
        for (let i = 0; i < res.data.length; i++) {
          const element = res.data[i];
          for (let j = 0; j < result.roleId.length; j++) {
            const item = result.roleId[j];
            if (element.roleId == item) {
              //console.log(this.$refs);
              this.$refs.multipleTable.toggleRowSelection(element, true);
            }
          }
        }
      });
    });
    this.getStoreList();
  },
  methods: {
    getStoreList() {
      this.$service.userGetStoreList().then(res => {
        if (res.code == "00000") {
          this.productOptions = res.data || [];
        }
      });
    },
    backClick() {
      this.$router.back();
    },
    goBack() {
      this.$router.back();
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.$service
            .userUpdate({
              userId: this.form.id,
              username: this.form.username,
              password: this.form.password,
              status: this.form.status1 == true ? "1" : "0",
              roleId: this.multipleSelection.toString(),
              duties: this.form.duties,
              description: this.form.description,
              consumername: this.form.consumername,
              storeId:this.form.storeId
            })
            .then(res => {
              if (res.code == "200") {
                this.$message({
                  message: "修改成功",
                  type: "success"
                });
                this.$refs[formName].resetFields();
                this.$emit("submit", false);
                this.$router.back();
              } else {
                this.$message({
                  message: res.message,
                  type: "warning"
                });
              }
            });
        }
      });
    },
    resetForm(formName) {
      this.$emit("submit", false);
      this.$refs[formName].resetFields();
    },
    indexMethod(index) {
      var index1 = index + 1;
      return index1++;
    },
    handleSelectionChange(val) {
      console.log(val);
      this.multipleSelection = [];
      for (let i = 0; i < val.length; i++) {
        const element = val[i];
        this.multipleSelection.push(element.roleId);
      }
    }
  }
};
</script>

<style lang="scss">
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}
.avatar {
  width: 100px;
  height: 100px;
  display: block;
}
</style>